# -*- coding:utf-8 -*-
#
# Copyright © 2013 Lucian Smith
# Based on the 'pylint' plugin
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)

"""Hello Code Analysis Plugin"""

from spyderlib.qt.QtGui import QVBoxLayout, QGroupBox, QLabel
from spyderlib.qt.QtCore import SIGNAL, Qt

# Local imports
from spyderlib.baseconfig import get_translation
_ = get_translation("p_hello", dirname="spyderplugins")
from spyderlib.utils.qthelpers import get_icon, create_action
from spyderlib.plugins import SpyderPluginMixin, PluginConfigPage

from spyderplugins.widgets.hellogui import HelloWidget


class HelloConfigPage(PluginConfigPage):
    def setup_page(self):
        settings_group = QGroupBox(_("Settings"))
        save_box = self.create_checkbox(_("Here's a tickybox to check!"),
                                        'tick_value', default=True)
        
        number_group = QGroupBox(_("Numbers"))
        number_spin = self.create_spinbox(_("Number of hellos: "),
                            _(" hellos"), 'hello_number', default=1,
                            min_=1, max_=10, step=1)

        results_group = QGroupBox(_("Results"))
        results_label1 = QLabel(_("Hello world results are not actually stored here:"))
        results_label1.setWordWrap(True)

        # Warning: do not try to regroup the following QLabel contents with 
        # widgets above -- this string was isolated here in a single QLabel
        # on purpose: to fix Issue 863
        results_label2 = QLabel(HelloWidget.DATAPATH)

        results_label2.setTextInteractionFlags(Qt.TextSelectableByMouse)
        results_label2.setWordWrap(True)

        settings_layout = QVBoxLayout()
        settings_layout.addWidget(save_box)
        settings_group.setLayout(settings_layout)

        number_layout = QVBoxLayout()
        number_layout.addWidget(number_spin)
        number_group.setLayout(number_layout)

        results_layout = QVBoxLayout()
        results_layout.addWidget(results_label1)
        results_layout.addWidget(results_label2)
        results_group.setLayout(results_layout)

        vlayout = QVBoxLayout()
        vlayout.addWidget(settings_group)
        vlayout.addWidget(number_group)
        vlayout.addWidget(results_group)
        vlayout.addStretch(1)
        self.setLayout(vlayout)


class Hello(HelloWidget, SpyderPluginMixin):
    """Create an editable window that says 'Hello World' (based on pylint)"""
    CONF_SECTION = 'hello'
    CONFIGWIDGET_CLASS = HelloConfigPage
    def __init__(self, parent=None):
        HelloWidget.__init__(self, parent=parent,
                              hello_number=self.get_option('hello_number', 5))
        SpyderPluginMixin.__init__(self, parent)
        
        # Initialize plugin
        self.initialize_plugin()
        
    #------ SpyderPluginWidget API --------------------------------------------
    def get_plugin_title(self):
        """Return widget title"""
        return _("Hello")
    
    def get_plugin_icon(self):
        """Return widget icon"""
        return get_icon('hello.png')
    
    def get_focus_widget(self):
        """
        Return the widget to give focus to when
        this plugin's dockwidget is raised on top-level
        """
        return self.outwidget
    
    def get_plugin_actions(self):
        """Return a list of actions related to plugin"""
        # Font
        return []

    def on_first_registration(self):
        """Action to be performed on first plugin registration"""
        self.main.tabify_plugins(self.main.inspector, self)
        self.dockwidget.hide()

    def register_plugin(self):
        """Register plugin in Spyder's main window"""
        self.connect(self, SIGNAL("edit_goto(QString,int,QString)"),
                     self.main.editor.load)
        self.connect(self, SIGNAL('redirect_stdio(bool)'),
                     self.main.redirect_internalshell_stdio)
        self.main.add_dockwidget(self)
        
        hello_act = create_action(self, _("Run hello world"),
                                   triggered=self.run_hello)
        hello_act.setEnabled(True)
        self.register_shortcut(hello_act, context="Hello world",
                               name="Run hello world", default="Alt-F10")
        
        self.main.source_menu_actions += [None, hello_act]
        self.main.editor.pythonfile_dependent_actions += [hello_act]

    def refresh_plugin(self):
        """Refresh hello widget"""
        pass
        
    def closing_plugin(self, cancelable=False):
        """Perform actions before parent main window is closed"""
        return True
            
    def apply_plugin_settings(self, options):
        """Apply configuration file's plugin settings"""
        self.hello_number = self.get_option('hello_number', 1)
        pass
        
    def run_hello(self):
        """Display the 'hello' plugin and add some Hello's"""
        if self.dockwidget and not self.ismaximized:
            self.dockwidget.setVisible(True)
            self.dockwidget.setFocus()
            self.dockwidget.raise_()
        HelloWidget.start(self)


#==============================================================================
# The following statements are required to register this 3rd party plugin:
#==============================================================================
PLUGIN_CLASS = Hello

